package sky.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import sky.domain.Airport;
import sky.service.AirportService;
import sky.service.FlightService;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Gauss
 * Date: 04.05.13
 * Time: 19:50
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class AirportController {

    @Autowired
    private AirportService airportService;

    protected Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    @ModelAttribute("showAuth")
    public Integer getAuth() {
        for (GrantedAuthority authority: getAuthentication().getAuthorities()) {
            if (authority.getAuthority().equals("ROLE_USER")) {
                return 1;
            }
            if (authority.getAuthority().equals("ROLE_ADMIN")) {
                return 2;
            }
        }
        return 0;
    }

    @RequestMapping(value = "/airports", method = RequestMethod.GET)
    public String mainAirports(Map<String, Object> map) {
        map.put("airportList", airportService.listAirport());
        map.put("NAirport", new Airport());
        return "airports";
    }

    @RequestMapping(value = "/airportAdd", method = RequestMethod.POST)
    public String addAirport(@ModelAttribute("airport") Airport airport, BindingResult result) {
        airportService.addAirport(airport);
        return "redirect:/airports";
    }

    @RequestMapping(value = "/airports/delete/{airportId}", method = RequestMethod.GET)
    public String deleteAirport(@PathVariable("airportId") Integer airportId) {
        airportService.removeAirport(airportId);
        return "redirect:/airports";
    }

    @RequestMapping(value = "/airports/edit/{airportId}", method = RequestMethod.GET)
    public String editAirport(@PathVariable("airportId") Integer airportId, Map<String, Object> map) {
        map.put("airport", airportService.getAirportById(airportId));
        return "editAirport";
    }

    @RequestMapping(value = "/airports/edit/*", method = RequestMethod.POST)
    public String editAirport(@ModelAttribute("airport") Airport airport, BindingResult result) {
        airportService.editAirport(airport);
        return "redirect:/index";
    }

    @RequestMapping(value = "/airport", method = RequestMethod.GET)
    public String airportInfo(@RequestParam("id") Integer id, Map<String, Object> map) {
        map.put("airport", airportService.getAirportById(id));
        return "airport";
    }
}
